<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>

<%-- <%@taglib uri="spring.tld" prefix="spring"%> --%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE html>
<html>
	<head>
    	<base href="<%=basePath%>">
    
    	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
		<meta charset="utf-8"><meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
		<title>用户管理</title>
		<meta name="keywords" content="">
		<link rel="shortcut icon" href="<%=basePath%>resources/images/favicon.ico" />
		<link href="resources/bootstrap/css/bootstrap-huan.css" rel="stylesheet">
		<link href="resources/font-awesome/css/font-awesome.min.css" rel="stylesheet">
		<link href="resources/css/style.css" rel="stylesheet">
		
		<link href="resources/css/exam.css" rel="stylesheet">
		<link href="resources/chart/morris.css" rel="stylesheet">
		<style type="text/css">
			.disable-btn, .enable-btn{
				text-decoration: underline;
			}
			.disable-btn, .enable-btn{
				cursor:pointer;
			}
		</style>
	</head>
	<body>
		<header>
			<div class="container">
				<div class="row">
					<div class="col-xs-5">
						<div class="logo">
							<h1><a href="#">网站管理系统</a></h1>
							<div class="hmeta">
								专注互联网在线考试解决方案
							</div>
						</div>
					</div>
					<div class="col-xs-7" id="login-info">
						<c:choose>
							<c:when test="${not empty sessionScope.SPRING_SECURITY_CONTEXT.authentication.principal.username}">
								<div id="login-info-user">
									
									<a href="user-detail/${sessionScope.SPRING_SECURITY_CONTEXT.authentication.principal.username}" id="system-info-account" target="_blank">${sessionScope.SPRING_SECURITY_CONTEXT.authentication.principal.username}</a>
									<span>|</span>
									<a href="j_spring_security_logout"><i class="fa fa-sign-out"></i> 退出</a>
								</div>
							</c:when>
							<c:otherwise>
								<a class="btn btn-primary" href="user-register">用户注册</a>
								<a class="btn btn-success" href="user-login-page">登录</a>
							</c:otherwise>
						</c:choose>
					</div>
				</div>
			</div>
		</header>
		<!-- Navigation bar starts -->

		<div class="navbar bs-docs-nav" role="banner">
			<div class="container">
				<nav class="collapse navbar-collapse bs-navbar-collapse" role="navigation">
					<ul class="nav navbar-nav">
						<li>
							<a href="#"><i class="fa fa-home"></i>网站首页</a>
						</li>
						<li>
							<a href="admin/question-list"><i class="fa fa-edit"></i>试题管理</a>
						</li>

						<li>
							<a href="admin/exampaper-list"><i class="fa fa-file-text-o"></i>试卷管理</a>
						</li>
						<li class="active">
							<a href="admin/user-list"><i class="fa fa-user"></i>用户管理</a>
						</li>
						<li>
							<a href="admin/field-list-1"><i class="fa fa-cloud"></i>题库管理</a>
						</li>
						<li>
							<a href="admin/sys-backup"><i class="fa fa-cogs"></i>网站设置</a>
						</li>
					</ul>
				</nav>
			</div>
		</div>

		<!-- Navigation bar ends -->

		<!-- Slider starts -->

		<div>
			<!-- Slider (Flex Slider) -->

			<div class="container" style="min-height:500px;">

				<div class="row">
					<div class="col-xs-3">
						<ul class="nav default-sidenav">
							<li class="active">
								<a> <i class="fa fa-list-ul"></i> 用户管理 </a>
							</li>
							<li>
								<a href="admin/add-user"> <i class="fa fa-list-ul"></i> 添加用户 </a>
							</li>
							<li>
								<a href="admin/user-record"> <i class="fa fa-list-ul"></i> 用户练习统计 </a>
							</li>
						</ul>
					</div>
					<div class="col-xs-9">
						<div class="page-header">
							<h1><i class="fa fa-list-ul"></i> 用户管理 </h1>
						</div>
						<div class="page-content row">

							
							<div id="question-list">
								<table class="table-striped table">
									<thead>
										<tr>
											<td>ID</td>
											<td>账号</td>
											<td>姓名</td>
											<td>电话</td>
											<!-- <td>单位</td>-->
											<td>部门</td>
											<td>专业</td>
											<td>注册时间</td>
											<td>状态</td>
											<td>操作</td>
										</tr>
									</thead>
									<tbody>
										<c:forEach items="${userList }" var="item">
											<tr>
												<td><span class="td-user-id">${item.id }</span></td>
												<td><span class="td-user-name">${item.username }</span></td>
												<td><span class="td-user-true-name">${item.truename }</span></td>
												<td><span class="td-user-phone">${item.phone }</span></td>
												<!--<td>${item.company }</td> -->
												<td><span class="td-user-department">${item.department }</span></td>
												<td><span class="td-user-field-name" data-id=“${item.fieldId}”>${item.fieldName }</span></td>
												<!-- <td></td> -->
												<td>
													<fmt:formatDate value="${item.create_date }" pattern="yyyy-MM-dd"/>
												</td>
												<td>
													<c:choose>
														<c:when test="${item.enabled == 1 }">
															<span class="label label-success">启用</span>
														</c:when>
														<c:when test="${item.enabled == 0 }">
															<span class="label label-danger">注销</span>
														</c:when>
														<c:otherwise>
															其他
														</c:otherwise>
													</c:choose>
												</td>
												<td>
													<c:choose>
														<c:when test="${item.enabled == 1 }">
															<span class="disable-btn" data-id="${item.id}">禁用</span>
															<a class="update-user">编辑</a>
														</c:when>
														<c:when test="${item.enabled == 0 }">
															<span class="enable-btn" data-id="${item.id}">启用</span>
															<a class="update-user">编辑</a>
														</c:when>
													</c:choose>
												</td>
											</tr>
										</c:forEach>
										
									</tbody><tfoot></tfoot>
								</table>
							</div>
							<div class="modal fade" id="change-property-modal" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel" aria-hidden="true">
													  <div class="modal-dialog">
													    <div class="modal-content">
													    	<div class="modal-header">
														        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
														        <h6 class="modal-title" id="myModalLabel">编辑用户</h6>
														     </div>
														     <div class="modal-body">
														     	<form id="question-add-form">
														     		<span id="add-update-user-id" style="display:none;"></span>
															     	<div class="form-line add-update-user-name">
																		<span class="form-label"><span class="warning-label">*</span>账号：</span>
																		<input type="text" class="df-input-narrow" disabled="disabled">
																		<span class="form-message"></span>
																	</div>
																	<div class="form-line add-update-true-name">
																		<span class="form-label"><span class="warning-label">*</span>姓名：</span>
																		<input type="text" class="df-input-narrow">
																		<span class="form-message"></span>
																	</div>
																	<div class="form-line exam-type" id="aq-course2">
																		<span class="form-label"><span class="warning-label">*</span>专业：</span>
																		<select id="user-type-select" class="df-input-narrow">
																			<c:forEach items="${fieldList}" var="item">
																				<option value="${item.fieldId}">${item.fieldName}</option>
																			</c:forEach>
																		</select><span class="form-message"></span>
																	</div>
																	<div class="form-line add-update-department">
																	<span class="form-label"><span class="warning-label">*</span>部门：</span>
																		<input type="text" class="df-input-narrow">
																		<span class="form-message"></span>
																	</div>
																	<div class="form-line add-update-phone">
																		<span class="form-label">电话：</span>
																		<input type="text" class="df-input-narrow">
																		<span class="form-message"></span>
																	</div>
																</form>
														     </div>
														     <div class="modal-footer">
				        										<button type="button" class="btn btn-default" data-dismiss="modal">关闭窗口</button>
				        										<button id="update-user-btn" type="button" class="btn btn-primary">确定修改</button>
				      										 </div>
													    </div>
													  </div>
								</div>
							<div id="page-link-content">
								<ul class="pagination pagination-sm">${pageStr}</ul>
							</div>

						</div>
					</div>
				</div>
			</div>
		</div>

		<footer>
			<div class="container">
				<div class="row">
					<div class="col-md-12">
						<div class="copy">
							<p>
								贵州邮电规划设计院 Copyright © <a href="http://www.gzcdc.com/" target="_blank">贵州邮电规划设计院</a> - <a href="." target="_blank">主页</a> | <a href="http://www.gzcdc.com/" target="_blank">关于我们</a> | <a href="http://www.gzcdc.com/" target="_blank">FAQ</a> | <a href="http://www.gzcdc.com/" target="_blank">联系我们</a>
							</p>
						</div>
					</div>
				</div>

			</div>

		</footer>

		<!-- Slider Ends -->

		<!-- Javascript files -->
		<!-- jQuery -->
		<script type="text/javascript" src="resources/js/jquery/jquery-1.9.0.min.js"></script>
		<script type="text/javascript" src="resources/js/all.js"></script>
		
		<!-- Bootstrap JS -->
		<script type="text/javascript" src="resources/bootstrap/js/bootstrap.min.js"></script>
		<script>
				$(".update-user").click(function() {
					$("#change-property-modal").modal({
						backdrop : true,
						keyboard : true
					});
					var tr = $(this).parent().parent();
					var name = tr.find(".td-user-name").text();
					var true_name = tr.find(".td-user-true-name").text();
					var department = tr.find(".td-user-department").text();
					var field_name = tr.find(".td-user-field-name").text();
					var phone = tr.find(".td-user-phone").text();
					var user_id = tr.find(".td-user-id").text();

					var count=$("#user-type-select").get(0).options.length;
    				 for(var i=0;i<count;i++)  {           
  						  if($("#user-type-select").get(0).options[i].text == field_name)  {  
            					$("#user-type-select").get(0).options[i].selected = true;
            			 		break;  
        				   }  
        			}

					$("#add-update-user-id").text(user_id);
					$(".add-update-user-name input").val(name);
					$(".add-update-true-name input").val(true_name);
					$(".add-update-department input").val(department);
					$(".add-update-phone input").val(phone);
				});
				$("#update-user-btn").click(function() {
					//var verify_result = question_list.verifyInput();
					
					var usre_id = $("#add-update-user-id").text();
						var data = new Object();
						data.id = usre_id;
						data.truename = $(".add-update-true-name input").val();
						data.phone = $(".add-update-phone input").val();
						data.department = $(".add-update-department input").val();
						data.fieldId = $("#user-type-select option:selected").val();
						if(data.truename == "" || data.department == ""){
							alert("用户名或部门不能为空");
							return;
						}
						$.ajax({
							headers : {
								'Accept' : 'application/json',
								'Content-Type' : 'application/json'
							},
							type : "POST",
							url : "admin/update-user",
							data : JSON.stringify(data),
							success : function(message, tst, jqXHR) {
								if (!util.checkSessionOut(jqXHR))
									return false;
								if (message.result == "success") {
									util.success("修改成功", function() {
										window.location.reload();
									});
								} else {
									util.error("操作失败请稍后尝试:" + message.result);
								}

							},
							error : function(jqXHR, textStatus) {
								util.error("操作失败请稍后尝试111111");
							}
						});
				});
				
				$(".disable-btn").click(function() {
					var message = "确定要禁用该用户吗？";
					var answer = confirm(message);
					if (!answer) {
						return false;
					}

					jQuery.ajax({
						headers : {
							'Accept' : 'application/json',
							'Content-Type' : 'application/json'
						},
						type : "GET",
						url : 'admin/disable-user/' + $(this).data("id"),
						success : function(message, tst, jqXHR) {
							if (!util.checkSessionOut(jqXHR))
								return false;
							if (message.result == "success") {
								util.success("操作成功！", function() {
									window.location.reload();
								});
							} else {
								util.error(message.result);
							}
						},
						error : function(jqXHR, textStatus) {
							util.error("操作失败请稍后尝试");
						}
					});

				});

				$(".enable-btn").click(function() {
					var message = "确定要启用该用户吗？";
					var answer = confirm(message);
					if (!answer) {
						return false;
					}
					jQuery.ajax({
						headers : {
							'Accept' : 'application/json',
							'Content-Type' : 'application/json'
						},
						type : "GET",
						url : 'admin/enable-user/' + $(this).data("id"),
						success : function(message, tst, jqXHR) {
							if (!util.checkSessionOut(jqXHR))
								return false;
							if (message.result == "success") {
								util.success("操作成功！", function() {
									window.location.reload();
								});
							} else {
								util.error(message.result);
							}
						},
						error : function(jqXHR, textStatus) {
							util.error("操作失败请稍后尝试");
						}
					});

				});
		</script>
	</body>
</html>